<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class CatesController extends Controller
{
    //add
    public function add()
    {	

    	$data = DB::table('cates')
    		-> select(['id','name','path',DB::raw("concat(path,',',id) as sort")])
    		->orderBy ('sort','asc')
    		->get();
    	foreach($data as $k => $v)
    	{
    		$count = substr_count($v -> path,',');
            
    		$str = str_repeat('|-------',$count);

    		$data[$k] -> name = $str.$v -> name;
    		
    	}

    
    	
    	return view('admin.cates.add',['data'=>$data]);
    }
    //insert
    public function insert(Request $request)
    {
    	$this -> validate($request,[

    		'name' => 'required',
    		],[
    		'name.required'=>'商品名不能为空',
    		]);
    	$data = $request ->except('_token');
    	if($request -> input('pid') == '0')
    	{
    		$data['path'] = '0';
    	}else
    	{
    		// 获得新加商品父类的path
    		$path = DB::table('cates') -> where('id',$request ->input('pid')) -> first() -> path;
    		//拼出新加商品的path
    		$data['path'] = $path.','.$request ->input('pid');
    	}
    	
    	$res = DB::table('cates') -> insert($data);
    	if($res)
    	{
    		return redirect('/admin/cates/index') -> with(['info' => '添加成功']);
    	}else
    	{
    		return back() -> with(['info' => '添加失败']);
    	}
    }

    //index
    public function index()
    {	
    	// select c1.*,c2.name as pname,CONCAT(c1.path,',',c1.id) as sort 
    // from cates as c1 LEFT JOIN cates as c2 on c1.pid = c2.id ORDER BY sort;
    	//查询数据表
    	$data = DB::table('cates as c1') 
    		->select('c1.*','c2.name as pname',DB::raw("concat(c1.path,',',c1.id) as sort"))
    		->LeftJoin ('cates as c2','c1.pid','=','c2.id')
    		->orderBy ('sort')
    		-> get();
    		// dd($data);
    		foreach($data as $k => $v)
    		{
	    		$count = substr_count($v -> path,',');
	    		$str = str_repeat('|-------',$count);

	    		$data[$k] -> pname = $str.$v -> pname;
	    		if(empty($data[$k] -> pname))
    			{
    				$data[$k] -> pname = '根分类';
    			}
    		}
    		

    	return view('Admin.cates.index',['data' => $data]);
    }
    //edit
    public function edit($id)
    {   //查询所要修改的分类
    	$array = DB::table('cates') -> where('id',$id) -> first();
        //查询所有的分类
        $data = DB::table('cates')
            -> select(['id','name','path',DB::raw("concat(path,',',id) as sort")])
            ->orderBy ('sort','asc')
            ->get();
        foreach($data as $k => $v)
        {
            $count = substr_count($v -> path,',');
            $str = str_repeat('|-------',$count);

            $data[$k] -> name = $str.$v -> name;
            
        }
    	return view('Admin.cates.edit',['arr' => $array,'data'=>$data]);
    }
    //update
    public function update(Request $request)
    {   
        $this -> validate($request,[

            'name' => 'required',
            ],[
            'name.required'=>'商品名不能为空',
            ]);
        //获得传过来的值
        $arr = $request -> except('_token');
        if($arr['pid'] == 0)
        {
            $arr['path']=0;
        }else
        {
             // 根据传来的pid查找父类
            $data = DB::table('cates')
            -> where('id',$arr['pid'])
            -> first();
            // 拼出新加商品的path
            $parentPath = $data -> path;
            $arr['path'] = $parentPath.','.$data ->id;
        }
       
        $res = DB::table('cates') -> where('id',$arr['id'])-> update($arr);
        if($res)
        {
            return redirect('/admin/cates/index') -> with(['info' => '添加成功']);
        }else
        {
            return back() -> with(['info' => '添加失败']);
        }

    }
    //delete
    public function delete($id)
    {
        $res = DB::table('cates') -> where('pid',$id) -> get();
        if($res)
        {   

            return back() -> with(['info' => '拥有子类不能删除']);
        }else
        {
            
            $res = DB::table('cates') -> delete($id);
            if($res)
            {
                return redirect('/admin/cates/index') -> with(['info' => '删除成功']);
            }else
            {
                return back() -> with(['info' => '删除失败']);
            }
        }

        
    }

    // ajaxupdate
    public function ajaxUpdate(Request $request)
    {
        $id = $request -> input('id');
        //查 询cates表当前分类的状态
        $data = DB::table('cates') -> where('id',$id) -> first();
        if($data -> status == '0')
        {
            $res = DB::table('cates')
                -> where('id',$id)
                -> update(['status' => '1']);
            if($res)
            {
                return response() -> json('0');
            }else
            {
                return response() -> jspn('1');
            }

        }else
        {
            $res = DB::table('cates') 
                -> where('id',$id) 
                -> update(['status' => '0']);
            if($res)
            {
                return response() -> json('2');
            }else
            {
                return response() -> jspn('3');
            }    
        }
        

    }


}


